package com.example.zy_22106107.controller;

import com.example.zy_22106107.bean.UserBean;
import com.example.zy_22106107.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 登录接口
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody UserBean user) {
        // 使用 UserBean 对象获取用户名和密码
        UserBean loginUser = userService.login(user.getUsername(), user.getPassword());
        if (loginUser != null) {
            return ResponseEntity.ok("登录成功");
        } else {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户名或密码错误");
        }
    }

    // 注册接口
    @PostMapping("/register")
    public ResponseEntity<String> register(@RequestBody UserBean user) {
        boolean isRegistered = userService.register(user.getUsername(), user.getEmail(), user.getPassword());
        if (isRegistered) {
            return ResponseEntity.ok("注册成功");
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("用户名已存在");
        }
    }


    // 删除论文接口
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteJournal(@PathVariable int id) {
        boolean isDeleted = userService.deleteJournalById(id);
        if (isDeleted) {
            return ResponseEntity.ok("论文删除成功");
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("论文不存在或删除失败");
        }
    }


    @GetMapping("/search")
    public ResponseEntity<?> searchJournal(
            @RequestParam(required = false) Long id,
            @RequestParam(required = false) String title
    ) {
        if (id != null) {
            UserBean journal = userService.searchJournalById(id);
            return journal != null ? ResponseEntity.ok(journal) : ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        } else if (title != null) {
            List<UserBean> journals = userService.searchJournalByTitle(title);
            return !journals.isEmpty() ? ResponseEntity.ok(journals) : ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("No search criteria provided");
        }
    }



    // 添加新论文接口
    @PostMapping("/add")
    public ResponseEntity<String> addJournal(@RequestBody UserBean journalBean) {
        boolean isAdded = userService.addJournal(journalBean);
        if (isAdded) {
            return ResponseEntity.ok("论文添加成功");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("论文添加失败");
        }
    }


    // 修改论文接口
    @PutMapping("/update/{id}")
    public ResponseEntity<String> updateJournal(@PathVariable int id, @RequestBody UserBean journalBean) {
        boolean isUpdated = userService.updateJournal(id, journalBean);
        if (isUpdated) {
            return ResponseEntity.ok("论文更新成功");
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("论文不存在或更新失败");
        }
    }
}
